package com.mentra.asg_client.service.core.handlers;

import android.util.Log;

import com.mentra.asg_client.service.legacy.interfaces.ICommandHandler;

import org.json.JSONObject;

import java.util.Set;

/**
 * Handler for OTA-related commands.
 * Follows Single Responsibility Principle by handling only OTA commands.
 */
public class OtaCommandHandler implements ICommandHandler {
    private static final String TAG = "OtaCommandHandler";

    public OtaCommandHandler() {
        // No dependencies needed for OTA command handling
    }

    @Override
    public Set<String> getSupportedCommandTypes() {
        return Set.of("ota_update_response");
    }

    @Override
    public boolean handleCommand(String commandType, JSONObject data) {
        try {
            switch (commandType) {
                case "ota_update_response":
                    return handleOtaUpdateResponse(data);
                default:
                    Log.e(TAG, "Unsupported OTA command: " + commandType);
                    return false;
            }
        } catch (Exception e) {
            Log.e(TAG, "Error handling OTA command: " + commandType, e);
            return false;
        }
    }

    /**
     * Handle OTA update response command
     */
    private boolean handleOtaUpdateResponse(JSONObject data) {
        try {
            boolean accepted = data.optBoolean("accepted", false);
            if (accepted) {
                Log.d(TAG, "Received ota_update_response: accepted, proceeding with OTA installation");
                // TODO: Trigger OTA installation here
                // This would typically involve starting the OTA updater service
                // or triggering the download/installation process
            } else {
                Log.d(TAG, "Received ota_update_response: rejected by user");
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "Error handling OTA update response", e);
            return false;
        }
    }
} 